layui.define(['table', 'form', 'admin', 'upload'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    //
    table.render({
        elem: '#LAY-props-manage'
        , url: '/admin/props/getlist'
        , cols: [[
            {field: 'id', title: 'ID', width: 70}
            , {field: 'name', title: '名称'}
            , {
                field: '', title: '图片', templet: function (d) {
                    if (d.icon) {
                        return '<img style="display: inline-block; width: auto; height: 100%;" layadmin-event="photosPreview" src="' + d.icon + '"  value="' + d.icon + '">';
                    }
                    return '无';
                }
            }
            , {field: 'weight', title: '权重'}
            , {field: 'type_text', title: '类型'}
            , {field: 'style', title: '样式'}
            , {
                field: '', title: '状态', templet: function (d) {
                    return d.status == 1 ? '启用' : '禁用';
                }
            }
            , {title: '操作', width: 160, align: 'center', fixed: 'right', toolbar: '#table-props-operation'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    //监听工具条
    table.on('tool(LAY-props-manage)', function (obj) {
        if (obj.event === 'edit') {
            var layerForm = layer.open({
                type: 2
                , title: '编辑'
                , content: '/admin/props/edit?id=' + obj.data.id
                , maxmin: true
                , area: ['650px', '500px']
            });
        }
    });

    //上传图片
    upload.render({
        url: '/admin/index/noOperation'
        , elem: '.LAY_iconImgUpload'
        , auto: true
        , choose: function (obj) {
            // 图片地址表单id
            var imginput_obj_id = this.idObjName;

            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: '/admin/activity/signForCos'
                , success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime
                                });
                            }
                        });
                        cos.putObject({
                            Bucket: res.data.bucket,
                            Region: res.data.region,
                            Key: 'images/' + res.data.filename + choosefile.name,
                            Body: choosefile,
                        }, function (err, data) {
                            layer.closeAll('loading');
                            if (err) {
                                console.log(err);
                                layer.msg("上传失败", {icon: 5});
                            } else {
                                if (data.statusCode == 200) {
                                    $('#' + imginput_obj_id).val("https://" + data.Location);
                                } else {
                                    layer.msg("图片上传失败", {icon: 5});
                                }
                            }
                        });
                    } else {
                        layer.closeAll('loading');
                        layer.msg(res.msg, {icon: 5});
                    }
                }
            })
        }
    });

    //查看图标
    admin.events.coverImgPreview = function (othis) {
        var pic_obj_id = othis.data('idname');
        var src = $('#' + pic_obj_id).val();
        layer.photos({
            photos: {
                "title": "查看图标" //相册标题
                , "data": [{
                    "src": src //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };

    //列表查看
    admin.events.photosPreview = function (othis) {
        let urls = othis.attr("value");
        let urlarr = new Array();
        $.each(urls.split(","), function (i, val) {
            urlarr.push({"src": val});
        });
        layer.photos({
            photos: {
                "title": "查看照片" //相册标题
                , "data": urlarr
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };


    exports('props', {})
});